/**
 * Created by LeungZ on 2016/10/17.
 */
class Ctrl {
    constructor(imageSrv,userSrv, appSrv,$rootScope, $scope, ctgNotification) {
        "ngInject";
        this._imageSrv = imageSrv;
        this._appSrv = appSrv;
        this._userSrv= userSrv;
        this._$scope=$scope;
        this._notification = ctgNotification;
        this.imageType = "002";
        this.service = $scope.service;
        if ($scope.service.appImg && $scope.service.appImg.imageName) {
            this.getImgVerList($scope.service.appImg.imageName);
            this.getConfigs($scope.service.appImg.ver);
            this.getConfigVers($scope.service.appImg.imageConfig);
            //  console.log($scope.service.appImg)
        } 
         this.user={
           userCode:$rootScope.user.sysUserCode   
        };
        this.page = {
            currentPage: 1,
            totalItems: 0,
            page:5
        };

        this. getIsAdmin();
    } 
    //是否管理员登录
   
    getIsAdmin(){
       this._imageSrv.isAdmin().then(data=>{
        // console.log(data);
        if(data==true){                      
              this._$scope.searchBtn=true;
              this._userSrv.getUserList({currentPage: this.page.currentPage}).then(data=>{
               console.log(data.list)
               this.service.username=data.list;
              })
            }else{
              this._$scope.searchBtn=false;  
            } 
       }).catch((data)=>{
            this._ctgNotification.error('获取用户数据有误.');
       }) 
    }
   
    getImageList() {
        this.verLoading = true;
        this._appSrv.getBuildImages(this.imageType).then((data) => {
            this.service.appImgList = data.list;
            if (data.list.length == 0) {
                this._notification.error("没有相关镜像，请到镜像管理新增镜像。");
            }
        }).catch(() => {
            this._notification.error("获取镜像列表有误。");
        }).finally(() => {
            this.verLoading = false;
        })
    }
    getImgVerList(imageName) {
        this.verLoading = true;
        this._imageSrv.getImageVers(imageName, this.imageType).then(data => {
            this.appImgVerList = data;
            if (this.service.appImg && this.service.appImg.ver) this.service.appImg.ver = _.findWhere(this.appImgVerList, { id: this.service.appImg.ver.id });
        }).catch(data => {
            this._notification.error("获取镜像版本数据有误, 请重新选择应用镜像");
        }).finally(() => {
            this.verLoading = false;
        })
    }

    getAppImgVer(imageName) {
        this.appImgVerList = null;
        // appImg.imageName = name;
        // appImg.ver = null;

        this.getImgVerList(imageName);
    }

    getConfigs(v) {
        this.configverLoading = true;
        this.configList = null;
        this._imageSrv.getConfigName({ dockerImageTagId: v.id }).then((data) => {
            this.configList = data;
            if (this.service.appImg && this.service.appImg.imageConfig) this.service.appImg.imageConfig = _.findWhere(this.configList, { configName: this.service.appImg.imageConfig.configName });
            if (data.length == 0) {
                this._notification.error("此镜像没有配置文件，请到镜像管理增加配置文件，或选择其他镜像。");
            }
        }).catch(() => {
            this._notification.error("获取配置文件有误");
        }).finally(() => {
            this.configverLoading = false;
        })
    }
    getConfigVers(v) {
        // console.log(v.dockerImageTagId)
        this.configverLoading = true;
        this.configVerList = null;
        this._imageSrv.getConfigVer({ dockerImageTagId: v.dockerImageTagId, config_name: v.configName }).then((data) => {
            this.configVerList = data;
            if (this.service.appImg && this.service.appImg.imageConfigVer) this.service.appImg.imageConfigVer = _.findWhere(this.configVerList, { configVersion: this.service.appImg.imageConfigVer.configVersion });
        }).catch(() => {
            this._notification.error("获取配置文件版本数据有误");
        }).finally(() => {
            this.configverLoading = false;
        })
    }
    getConfigsData(v) {
        this.configverLoading = true;
        this._imageSrv.getConfigsData({ dockerImageTagId: v.dockerImageTagId, config_name: v.configName, config_version: v.configVersion }).then((data) => {
            var config = JSON.parse(data[0].config);
            angular.forEach(config, (en) => {
                en.hide = true; //添加一个隐藏属性
                delete en.remark;
            });
            this.service.envs = config;
        }).catch(() => {
            this._notification.error("获取配置文件环境变量数据有误。");
        }).finally(() => {
            this.configverLoading = false;
        })
    }
}
export default Ctrl